<template>
  <div>
    <div class="supplier" v-if="!operlogAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          class="btn search"
          @click="handleQuery"
          v-hasPermi="['system:logininfor:query']"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:logininfor:remove']"
          class="btn delete"
          @click="handleClean()"
          ><el-icon class="icon" color="#fff"> <Delete /> </el-icon
          >清空</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入登录地址"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </div>
        <div class="search-item">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </div>
        <div class="search-item">
          <el-select
            v-model="queryParams.status"
            placeholder="登录状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 1, 1, 23, 59, 59),
            ]"
          ></el-date-picker>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        height="64vh"
        border
        ref="logininforRef"
        v-loading="loading"
        :data="logininforList"
        @selection-change="handleSelectionChange"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
        :header-cell-style="{ 'text-align': 'center', background: 'red' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="60px"
          align="center"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-hasPermi="['system:logininfor:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column label="访问编号" align="center" prop="infoId" />
        <el-table-column
          label="用户名称"
          align="center"
          prop="userName"
          :show-overflow-tooltip="true"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        />
        <el-table-column label="登录状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_common_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="描述"
          align="center"
          prop="msg"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="访问时间"
          align="center"
          prop="accessTime"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
          width="180"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.accessTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="Logininfor">
import {
  list,
  delLogininfor,
  cleanLogininfor,
  unlockLogininfor,
} from "@/api/system/logininfor";
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const { sys_common_status } = proxy.useDict("sys_common_status");

const logininforList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const selectName = ref("");
const total = ref(0);
const dateRange = ref([]);
const defaultSort = ref({ prop: "accessTime", order: "descending" });

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 50,
  ipaddr: undefined,
  userName: undefined,
  status: undefined,
  orderByColumn: undefined,
  isAsc: undefined,
});

/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      logininforList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.pageNum = 1;
  proxy.$refs["logininforRef"].sort(
    defaultSort.value.prop,
    defaultSort.value.order
  );
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.infoId);
  multiple.value = !selection.length;
  single.value = selection.length != 1;
  selectName.value = selection.map((item) => item.userName);
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}

const handleOperate = async (type, row) => {
  switch (type) {
    case "delete":
      ElMessageBox.confirm("此操作将删除该日志，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await delLogininfor(row.infoId);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            handleQuery();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有登录日志数据项?")
    .then(function () {
      return cleanLogininfor();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}
/** 解锁按钮操作 */
function handleUnlock() {
  const username = selectName.value;
  proxy.$modal
    .confirm('是否确认解锁用户"' + username + '"数据项?')
    .then(function () {
      return unlockLogininfor(username);
    })
    .then(() => {
      proxy.$modal.msgSuccess("用户" + username + "解锁成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/logininfor/export",
    {
      ...queryParams.value,
    },
    `config_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
<style>
.confirmBtnClass {
  color: #fff;
  width: 90px;
  height: 34px;
  background: #007cef;
}
.confirmBtnClass:hover,
.confirmBtnClass:active,
.confirmBtnClass:focus {
  color: #fff;
  background: #007cef;
}
.cancelBtnClass {
  border: 1px solid #999999;
  color: #333333;
  width: 90px;
  height: 34px;
  background: #f3f3f3;
}
.cancelBtnClass:hover,
.cancelBtnClass:active,
.cancelBtnClass:focus {
  border: 1px solid #999999;
  color: #333333;
  background: #f3f3f3;
}
</style>
<style lang="scss" scoped>
:deep(.table-header) {
  background-color: #f8faff !important;
}

.supplier {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  .search {
    display: flex;
    margin-bottom: 20px;
    .search-item {
      margin-right: 20px;
    }
    .search-input {
      width: 200px;
      height: 34px;
    }
  }
  /* .search-button {
    position: absolute;
    top: -45px;
    right: 60px;
    display: flex;
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;
      height: 34px;
      padding: 10px;
      // margin-right: 20px;
      font-size: 14px;
      border-radius: 4px;
      user-select: none;
      cursor: pointer;
    }
    .btn.search {
      color: #fff;
      background: rgba(0, 124, 239, 1);
      &:hover {
        background: rgba(0, 124, 239, 0.85);
      }
    }
    .btn.delete {
      border: none;
      color: #fff;
      background: rgba(245, 108, 108, 1);
      &:hover {
        background: rgba(245, 108, 108, 0.85);
      }
    }
    .btn.reset {
      color: rgba(43, 193, 123, 1);
      background: rgba(43, 193, 123, 0.05);
      border: 1px solid rgba(43, 193, 123, 1);
      &:hover {
        background: rgba(43, 193, 123, 0.2);
      }
    }
    .icon {
      margin-right: 6px;
    }
  } */
  .table-operate {
    display: flex;
    justify-content: space-around;
    .table-icon {
      width: 20px;
      height: 20px;
      cursor: pointer;
    }
  }
  .pagination {
    margin-top: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
  :deep(.el-table) {
    th.el-table__cell {
      font-size: 14px;
      font-weight: 400;
      height: 40px;
      color: #333333;
      background: #f8faff !important;
    }
    // 表格高度
    .el-table__cell {
      padding: 0;
      height: 40px;
    }
  }
  :deep(.el-table--large .cell) {
    padding: 0 20px 0 20px !important;
  }
}
</style>
